package org.zzq.my12306.frameworks.starter.base.init;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;

/**
 * 应用初始化后置处理器，防止 Spring 事件被多次执行
 */
@RequiredArgsConstructor
public class ApplicationContentPostProcessor implements ApplicationListener<ApplicationReadyEvent> {
    private final ApplicationContext applicationContext;

    /**
     * 执行标记，确保 Spring 事件 ApplicationReadyEvent 有且执行一次
     */
    private boolean executeOnlyOne = true;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        synchronized(ApplicationContentPostProcessor.class) {
            applicationContext.publishEvent(new ApplicationInitializingEvent(this));
            executeOnlyOne = false;
        }
    }
}
